package com.itheima.a_ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestIoC {
	
	@Test
	public void demo01(){
		//之前开发 接口=实现类
		UserService userService = new UserServiceImpl();
		userService.yuehui();
		/*UserService userService = new ZhansanUserServiceImpl();
		UserDao userDao = new LisiUserDaoImpl();
		userDao.ganhuo();*/
		
	}
	
	@Test
	public void demo02(){
		//从spring容器获得  不修改代码，直接配置文件中取出来userServiceId，
		//spring 相当于创建了对象 UserService userService = new UserServiceImpl();
		
		//1 获得容器
		String xmlPath = "com/itheima/a_ioc/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		//2获得内容 --不需要自己new，都是从spring容器获得
		UserService userService = (UserService) applicationContext.getBean("userServiceId2");
		userService.yuehui();
		
	}
	@Test
	public void demo03(){
		//从spring容器获得  不修改代码，直接配置文件中取出来userServiceId，
		//spring 相当于创建了对象 UserService userService = new UserServiceImpl();		
		//1 获得容器
		String xmlPath = "com/itheima/a_ioc/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		//2获得内容 --不需要自己new，都是从spring容器获得
		UserDao userdao = (UserDao) applicationContext.getBean("userDaoId3");
		userdao.ganhuo();
		
/*	类似下面这个功能
		UserDao userDao = new ZhansanUserDaoImpl();
		userDao.ganhuo();*/
		
	}
	@Test
	public void demo04(){
		//从spring容器获得  不修改代码，直接配置文件中取出来userServiceId，
		//spring 相当于创建了对象 UserService userService = new UserServiceImpl();		
		//1 获得容器
		String xmlPath = "com/itheima/a_ioc/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		//2获得内容 --不需要自己new，都是从spring容器获得
		UserDao userdao = (UserDao) applicationContext.getBean("userDaoId4");
		userdao.ganhuo();
	
		
	}
	@Test
	public void demo05(){
		//从spring容器获得  不修改代码，直接配置文件中取出来userServiceId，
		//spring 相当于创建了对象 UserService userService = new UserServiceImpl();		
		//1 获得容器
		String xmlPath = "com/itheima/a_ioc/beans.xml";
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		//2获得内容 --不需要自己new，都是从spring容器获得
		UserService userservice = (UserService) applicationContext.getBean("userServiceId5");
		userservice.yuehui();
	
		
	}
}



